<template>
  <section class="todoapp">
    <todo-header @addState="hAddState"></todo-header>
    <todo-main @delState="hDelState" :list="showList" @changeState="hChangeState"></todo-main>
    <todo-footer @choseState="hChoseState" @clearState="hClearState" :list="showList"></todo-footer>
  </section>
</template>

<script>
import TodoHeader from './components/TodoHeader.vue';
import TodoMain from './components/TodoMain.vue';
import TodoFooter from './components/TodoFooter.vue';
export default {
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  data() {
    return {
      list: [
        { id: 1, name: '吃饭', isDone: true },
        { id: 2, name: '睡觉', isDone: false },
        { id: 3, name: '打豆豆', isDone: true }
      ],
      type:'all'
    }
  },
  methods: {
    //获得需要改变状态的id
    //并修改对应id的isDone，取反使之改变状态
    hChangeState(id) {
      // console.log(id);
      const index = this.list.find(item => item.id === id)
      index.isDone = !index.isDone
    },
    //添加功能
    hAddState(info) {
      this.list.push({
        id: Date.now(),
        name: info,
        isDone: false
      })
    },
    hDelState(id) {
      // console.log(id);
       // 把id过滤掉
      this.list = this.list.filter(item => item.id != id)
    },
    hClearState(){
      // console.log(111);
      this.list = this.list.filter(item => !item.isDone)
    },
    hChoseState(type){
      this.type = type
    }
  },
  computed:{
    showList(){
      if(this.type === 'yes'){
        return this.list.filter(item => item.isDone)
      }
      else if(this.type === 'no'){
        return this.list.filter(item => !item.isDone)
      }
      else{
        return this.list
      }
    }
  }
}
</script>

<style>

</style>
